package org.shagin.jfin.date.cond;

import java.util.Calendar;
import java.util.Date;

import org.shagin.jfin.date.DateUtils;

/**
 * The Class Yearly.
 */
@SuppressWarnings("serial")
public class Yearly extends DateCondition {

    private int month = -1;
    private int day = -1;

    /**
     * Instantiates a new yearly.
     *
     * @param month the month
     * @param day the day
     */
    public Yearly(int month, int day) {
        this.month = month;
        this.day = day;
    }

    /**
     * {@inheritDoc}
     */
    public boolean validDate(Date date) {
        if (!super.validDate(date))
            return false;

        boolean result = false;
        if (month > 0) {
            int m = getDateMonth(date);
            int d = getDateDay(date);
            if ((month == m) && (day == d)) {
                result = true;
            }
        }
        return result;
    }

    /**
     * Gets the month.
     * 
     * @param date the date
     * @return the month
     */
    private int getDateMonth(Date date) {
        cal.setTime(date);
        return cal.get(Calendar.MONTH);
    }

    /**
     * Gets the date day.
     *
     * @param date the date
     * @return the date day
     */
    private int getDateDay(Date date) {
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "Ежегодно " + toMonthDays(new int[]{month}) + " "
                + toString(range);
    }

    /**
     * To month days.
     * 
     * @param array the array
     * @return the string
     */
    public String toMonthDays(int[] array) {
        String result = "";
        for (int i = 0; i < array.length; i++) {
            result += DateUtils.getMonth(array[i]) + " " + day
                    + (i < array.length - 1 ? "," : "");
        }
        return result;
    }

    /**
     * Gets the month.
     *
     * @return the month
     */
    public int getMonth() {
        return month;
    }

    /**
     * Sets the month.
     *
     * @param month the new month
     */
    public void setMonth(int month) {
        this.month = month;
    }

    /**
     * Gets the day.
     *
     * @return the day
     */
    public int getDay() {
        return day;
    }

    /**
     * Sets the day.
     *
     * @param day the new day
     */
    public void setDay(int day) {
        this.day = day;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String format() {
        return "Yearly(" + month + "," + day + ")";
    }
}
